<template>
  <el-dialog :visible.sync="dialog"
             :title="title"
             @closed="clearFormData"
             class="newApps-tabs-layout"
             width="620px"
             append-to-body>
    <div>
      <div class="filter_condition_lf"
           style="width:435px">
        <div class="filter_condition"> Filter</div>
        <div>
          <el-input placeholder="Search"
                    @input="checkedName"
                    v-model="searchVal">
            <i slot="prefix"
               class="el-input__icon el-icon-search"></i>
          </el-input>
        </div>
      </div>
    </div>
    <div class="widget_data_list"
         style="height:400px"
         v-loading="widgetLoading">
      <el-checkbox-group v-model="gadgetsCopy"
                         @change="handleCheckedCitiesChange">
        <el-checkbox v-for="(val) in  businessTypeListrecord.slice((currentPage-1)*5,currentPage*5)"
                     :label="val.resourceId"
                     :key="val.resourceId"
                     :class="'checked'?'el-checkted':''">
          <div class="item_widget2 flex-1 flex flex-col justify-between ">
            <div class="flex-1  truncate">
              <EllipsisVessel placement="right"
                              :text="val.resourceName" />
            </div>
            <div class="flex-1  truncate">
              <EllipsisVessel placement="right"
                              :text="val.description" />
            </div>
          </div>
          <!-- <div class="item_widget2">
            <div class="item_widget_content flex flex-col">
              <div class="flex-1 truncate">
                <EllipsisVessel placement="right" :text="val.resourceName"/>
              </div>
              <div class="flex-1 truncate">
                <EllipsisVessel placement="right" :text="val.description"/>
              </div>
            </div>
          </div> -->
        </el-checkbox>
      </el-checkbox-group>
    </div>
    <div>
      <el-pagination layout="prev, pager, next"
                     :total="businessTypeListrecord.length"
                     :page-size="5"
                     :page-count='currentPage'
                     :current-page='currentPage'
                     @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <template slot="footer">
      <el-button size="small"
                 @click="clearFormData">{{ $t('Cancel') }}</el-button>
      <el-button type="primary"
                 size="small"
                 @click="addGadgetToPageGroup()">{{ $t('Add') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  props: {
    gadgets: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      dialog: false,
      title: '',
      searchVal: '',
      currentPage: 1,
      dataForm: {
        dataset: ''
      },
      businessTypeListrecord: [],
      widgetLoading: false,
      userGadgetList: [],
      gadgetsCopy: []
    }
  },
  watch: {
    userGadgetList (val) {
      this.widgetLoading = false
      this.businessTypeListrecord = this.filterHasWidget(val)
    },
    dialog (newVal) {
      if (newVal) {
        // this.getBotList()
        this.widgetLoading = true
        this.gadgetsCopy = JSON.parse(JSON.stringify(this.gadgets))
      }
    },
    '$store.state.bot.routeMeta': function (val) {
      this.getDashboardLayout()
    }
  },
  computed: {
    GadgetList: {
      get () {
        let tempList = []
        tempList = this.userGadgetList
        return tempList
      },
      set (newVal) {
        this.businessTypeListrecord = newVal
      }
    },
    businessTypeList () {
      return window.SITE_CONFIG['dictList'].find(e => e.dictType === 'datasets_type_list').dataList
    }
  },
  methods: {
    clearFormData () {
      this.currentPage = 1
      this.searchVal = ''
      this.dataForm.dataset = ''
      this.businessTypeListrecord = []
      this.dialog = false
    },
    checkedName (val) {
      this.currentPage = 1
      if (this.businessTypeForWidget === 'Template') {
        this.GadgetList = this.filterHasWidget(this.userGadgetList).filter(e => e.businessType === 'Template').filter(e => e.name.toLowerCase().indexOf(val.toLowerCase()) !== -1)
      } else {
        this.GadgetList = this.filterHasWidget(this.userGadgetList).filter(e => e.name.toLowerCase().indexOf(val.toLowerCase()) !== -1)
      }
    },
    handleCheckedCitiesChange (value) {
      this.checkedGauge = value
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage
    },
    addGadgetToPageGroup () {
      var arr = []
      this.checkedGauge.forEach(item => {
        var res = this.userGadgetList.find(i => i.id === item)
        if (res) arr.push(res)
      })
      if (this.title === 'Select Bot') {
        arr.forEach(item => {
          item.resourceMenu = 2
          item.resourceType = 2
        })
        this.$emit('addBotToPage', arr)
      } else if (this.title === 'Select Dashboard') {
        arr.forEach(item => {
          item.resourceMenu = 1
          item.resourceType = 1
        })
        this.$emit('addReportToPage', arr)
      } else if (this.title === 'Select Setting') {
        arr.forEach(item => {
          item.resourceMenu = 3
          item.resourceType = 1
        })
        this.$emit('addReportToPage', arr)
      }
      this.dialog = false
      this.checkedGauge = []
    },
    filterHasWidget (tempList) {
      if (this.businessTypeForWidget !== 'MSPbots Internal') {
        if (this.businessTypeForWidget === 'Template') {
          tempList = this._.reject(tempList, { businessType: 'Custom' })
        }
        tempList = this._.reject(tempList, { businessType: 'MSPbots Internal' })
      }
      let arr = []
      let tempListArr = []

      this.gadgetsCopy.forEach(element => {
        if (element && element.i) {
          arr.push(element.i)
        }
      })
      tempList.forEach(element => {
        if (arr.indexOf(element.id) === -1) {
          tempListArr.push(element)
        }
      })
      return tempListArr
    },
    // getSaveGadgetList () {
    //   const params = {
    //     reuse: true,
    //     tenantCode: this.$store.state.user.tenantCode,
    //     showAllIntegration: true,
    //     internal: this.businessTypeForWidget === 'MSPbots Internal'
    //   }
    //   this.$interface.reports.getReportDataListApi(params)
    //     .then(({
    //       data: res
    //     }) => {
    //       if (res.code !== 0) {
    //         return this.$message.error(res.msg)
    //       }
    //       this.userGadgetList = res.data
    //     })
    //     .catch(() => { })
    // },
    getBotList () {
      const params = {
        tenantCode: this.$store.state.user.tenantCode,
        queryType: 'REFRESHCACHE',
        disable: true,
        showAllIntegration: false,
        businessType: 'Custom',
        internal: this.businessTypeForWidget === 'MSPbots Internal'
      }
      this.$interface.bots.getBotListApi(params)
        .then(({
          data: res
        }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          res.data = res.data.filter(i => {
            return i.version === '2' && (i.status === 'Published' || i.status === 'Alpha')
          })
          this.$emit('getbotList', res.data)
          res.data.forEach(item => {
            item.resourceId = item.id
            item.resourceName = item.name
          })
          this.userGadgetList = res.data
        })
        .catch(() => { })
    },
    getReportList () {
      const params = {
        queryType: 'REFRESHCACHE',
        showAllIntegration: false,
        internal: false,
        resourceType: 0
      }
      this.$interface.reports.getReportList(params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          res.data = res.data.filter(i => {
            return i.status === 'Published' || i.status === 'Alpha'
          })
          this.$emit('getRepostList', res.data)
          res.data.forEach(item => {
            item.resourceId = item.id
            item.resourceName = item.name
          })
          this.userGadgetList = res.data
        }).catch(() => { })
    },
    getNavLists () {
      // arr.forEach(item => {
      //   item.resourceId = item.id
      //   item.resourceName = item.name
      // })
      // this.userGadgetList = res.data
      this.widgetLoading = false
    }
  }
}
</script>

<style lang="scss">
.newApps-tabs-layout {
  // .select_widget_filter_condition {
  //   background: url("../../../assets/select_widget-filter-icon.png");
  //   background-repeat: no-repeat;
  //   background-position: center left;
  //   background-size: 14px 14px;
  //   padding-left: 20px;
  // .filter_condition_lf {
  //   .filter_condition {
  //     margin-top: 8px;
  //     margin-left: 5px;
  //   }
  // }
  // .filter_condition_lf > div {
  //   float: left;
  //   width: 150px;
  //   margin-right: 10px;
  // }
  // .filter_condition_lf > div:nth-child(1) {
  //   width: auto;
  // }
  // }
  // .select_widget_filter_condition::after {
  //   content: ".";
  //   clear: both;
  //   display: block;
  //   overflow: hidden;
  //   height: 0;
  // }
  .widget_data_list {
    margin-top: 20px;
    min-height: 20px;
    .el-checkbox-group {
      > .is-checked {
        border: 1px solid #138dc7;
      }
    }
    .el-checkbox {
      display: block;
      margin-right: 0;
      background: #ffffff;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      margin-top: 10px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .el-checkbox__input {
        display: block;
        width: 54px;
        text-align: center;
      }
      .el-checkbox__label {
        flex: 1;
        display: block;
        height: 100%;
        padding-left: 0;
        width: 0;
        .item_widget2 {
          // background: url("~@/assets/grid.png");
          background-size: 48px 48px;
          background-repeat: no-repeat;
          background-position: left center;
          // padding-left: 54px;
          padding-top: 5px;
          padding-bottom: 5px;

          height: 70px;
          > div {
            line-height: 28px;
          }
          > div:nth-child(1) {
            color: #000;
            font-size: 14px;
          }
          > div:nth-child(2) {
            font-size: 14px;
            color: rgba(0, 0, 0, 0.49);
          }
          .item_widget_ri {
            float: right;
            width: 53px;
            height: 100%;
            text-align: center;
            .item {
              height: 14px;
              width: 14px;
              border: 0;
              // background: url("../../../assets/select_widget_eye.png");
              background-size: 14px 14px;
              background-repeat: no-repeat;
              background-position: center;
            }
          }
          position: relative;
        }
        // .item_widget2::after {
        //   content: "";
        //   clear: both;
        //   display: block;
        //   overflow: hidden;
        //   position: absolute;
        //   right: 53px;
        //   border-right: 1px solid #f0f0f0;
        //   height: 80%;
        // }
      }
    }
    .el-checkbox::after {
      content: ".";
      clear: both;
      display: block;
      overflow: hidden;
      height: 0;
    }
  }
  .el-pagination {
    text-align: center;
  }
  .layoutTitle {
    font-size: 12px;
    word-break: keep-all;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .layoutTitle:hover {
    overflow: visible;
  }

  .vue-grid-item.vue-grid-placeholder {
    background: #f2f8ff;
    border: 1px solid #38f;
    opacity: 1;
  }

  .layoutJSON {
    background: #ddd;
    border: 1px solid black;
    margin-top: 10px;
    padding: 10px;
  }

  .eventsJSON {
    background: #ddd;
    border: 1px solid black;
    margin-top: 10px;
    padding: 10px;
    height: 100px;
    overflow-y: scroll;
  }

  .columns {
    -moz-columns: 120px;
    -webkit-columns: 120px;
    columns: 120px;
  }

  .vue-resizable-handle {
    z-index: 5000;
    position: absolute;
    width: 20px;
    height: 20px;
    bottom: 0;
    right: 0;
    background: url("");
    background-position: bottom right;
    padding: 0 3px 3px 0;
    background-repeat: no-repeat;
    background-origin: content-box;
    box-sizing: border-box;
    cursor: se-resize;
  }

  .editmode:not(.vue-grid-placeholder) {
    border: 1px solid #38f;
    opacity: 0.5;
  }

  .vue-grid-item.resizing {
    opacity: 0.9;
  }

  .vue-grid-item .no-drag {
    height: 100%;
    width: 100%;
  }

  .vue-grid-item .minMax {
    font-size: 12px;
  }

  .vue-grid-item .add {
    cursor: pointer;
  }

  .vue-draggable-handle {
    position: absolute;
    width: 20px;
    height: 20px;
    top: 0;
    left: 0;
    background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>")
      no-repeat;
    background-position: bottom right;
    padding: 0 8px 8px 0;
    background-repeat: no-repeat;
    background-origin: content-box;
    box-sizing: border-box;
    cursor: pointer;
  }

  .layout-item {
    text-align: center;
    margin-bottom: 15px;
    padding: 0 5px;
  }

  .layout-item .bottom {
    position: relative;
    margin-top: 15px;
  }

  .layout-item .bottom span {
    display: block;
    float: left;
    width: 50%;
    margin-top: 12px;
    text-align: left;
  }

  .layout-item .bottom button {
    padding-top: 0;
  }

  .top-toolbar {
    position: relative;
    margin-top: -15px;
    height: 35px;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    background-color: #fff;
    margin-left: -15px;
    padding-top: 10px;
    width: calc(100% + 30px);
  }

  .top-toolbar .top-toolbar-title {
    padding-left: 25px;
  }

  .btnBox {
    height: 16px;
    padding: 0px 10px;
    background: #c4ebf3;
    position: absolute;
    top: -15px;
    right: 24px;
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
  }

  .showBtn {
    font-size: 18px;
    color: #00c4ed;
  }

  .closeBtn {
    font-size: 18px;
    position: absolute;
    top: 8px;
    right: 11px;
  }
}
</style>
<style lang="scss" scoped>
::v-deep .el-dialog__header {
  display: block !important;
}
::v-deep .el-dialog__body {
  padding: 20px !important;
}
.more_text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap !important;
}
</style>
